﻿using ClusterAwareAdmin;
using ClusterAwareProperties;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace ClusterAwareAdminTest
{
    
    
    /// <summary>
    ///This is a test class for NetworkAdminTest and is intended
    ///to contain all NetworkAdminTest Unit Tests
    ///</summary>
    [TestClass()]
    public class NetworkAdminTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        private static ClusterManagerAdmin _manager;

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion

        /// <summary>
        ///A test for Rename
        ///</summary>
        [TestMethod]
        public void RenameTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkAdmin(_manager);
            foreach (var net in target.GetNetworks().Select(n => n.Name).ToArray())
            {
                target.Rename(net,net + "rename");
                Assert.AreEqual(true, target.ExistNetwork(net + "rename"));   
            }
            _manager.CacheInstances = false;
            foreach (var net in target.GetNetworks().Select(n => n.Name).ToArray())
            {
                target.Rename(net,net.Replace("rename",""));
                Assert.AreEqual(true, target.ExistNetwork(net.Replace("rename", "")));
            }
        }

        /// <summary>
        ///A test for NetworkAdmin Constructor
        ///</summary>
        [TestMethod]
        public void NetworkAdminConstructorTest()
        {
            new NetworkAdmin(_manager);
        }
    }
}
